<?php

/*
	工单列表（工作人员）
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return['types']=$this->task_types;
$return['statuss']=$this->task_status;
if($_RQ['op']=='list'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid']);
	//类型。1表示开发工单，2表示bug修复工单，3表示客户异常问题处理
	if(!empty($_RQ['groupid'])){
		$params['groupid']=intval($_RQ['groupid']);
		$return['params']['groupid']=$_RQ['groupid'];
	}
	if(!$_RQ['islog']){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=intval($_RQ['status']);
	}
	//工单名称
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}	
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_workliu_task',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	foreach($list as &$row){
		$row['groupid']=pdo_getcolumn('jy_workliu_taskgroup',array('id'=>$row['groupid']),'title');
		$row['getname']=pdo_getcolumn('jy_scrm_worker',array('id'=>$row['workerid']),'name');
		$row['createtime']= date('Y/m/d H:i:s',$row['createtime']);
	}
	$return['groups']=pdo_getall('jy_workliu_taskgroup',array(),array('id','title'));
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['receive_types']=array(
		'1'=>'管理员指派',
		'2'=>'自己领取',
		'3'=>'发布者指派',
	);
	exi($return);
}

if($_RQ['op']=='edit'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['title']){
			exi('请输入标题','error');
		}
		if(!$post['content']){
			exi('请输入内容','error');
		}
		if(!$post['groupid']){
			//exi('请选择组别','error');
		}
		$data=array(
			'title'=>trim($post['title']),
			'groupid'=>intval($post['groupid']),
			'content'=>trim($post['content']),
			'image'=>ijson_encode($post['image']),
			'credit'=>intval($post['credit']),
			'jiaji'=>intval($post['jiaji']),
			'setmid'=>$_SESSION['uid'],
			'ordersn'=>get_ordersn('jy_workliu_task','ordersn'),
		);
		$id=intval($post['id']);
		if($id){
			$tcpid = pdo_getcolumn('jy_workliu_task',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id),'tcpid');
			$tcplog = array('msg'=>trim($post['content']),'image'=>trim($post['image']));
			pdo_update('jy_workliu_task',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
			pdo_update('jy_workliu_tcplog',$tcplog,array('uniacid'=>$_SESSION['uniacid'],'tcpid'=>$tcpid));
		}else{
			$data['createtime']=TIMESTAMP;
			$data['setmid']=$_SESSION['uid'];
			$data['uniacid']=$_SESSION['uniacid'];
			pdo_insert('jy_workliu_tcp',array('uniacid'=>$_SESSION['uniacid']));
			$tcpid=pdo_insertid();
			$data['tcpid']=$tcpid;
			$basic=$this->plugin_setting('basic');
			$data['workerid']=$basic['workerid'];
			pdo_insert('jy_workliu_task',$data);
			$tcplog = array('uniacid'=>$_SESSION['uniacid'],'setmid'=>$_SESSION['uid'],'tcpid'=>$tcpid,'msg'=>trim($post['content']),'image'=>trim($post['image']),'createtime'=>TIMESTAMP);
			pdo_insert('jy_workliu_tcplog',$tcplog);
		}
		exi('操作成功！','','list');
	}
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_workliu_task',array('id'=>$id));
		$item['image']=$item['image']?json_decode($item['image'],true):array();
	}else{
		$item=array();
	}
	$return['groups']=pdo_getall('jy_workliu_taskgroup',array(),array('id','title'),'id');
	$return['item']=$item;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择工单！','error');
	}
	$item=pdo_get('jy_workliu_task',array('id'=>$id));
	$log=pdo_getall('jy_workliu_tcplog',array('tcpid'=>$item['tcpid']));
	$item['getname']=pdo_getcolumn('jy_scrm_worker',array('id'=>$item['workerid']),'name');
	$item['getname']=$item['getname']?:'匿名';
	if($log){
		foreach($log as &$v){
			if($v['setmid']==$item['setmid']){
				$v['isme']=1;
			}
			$v['createtime']=date('m-d H:i',$v['createtime']);
		}
	}
	$item['image']=$item['image']?json_decode($item['image'],true):array();
	$item['assess'] = $item['assess']? $item['assess'] : '还未审核！';
	$return['item']=$item;
	$return['log']=$log;
	$return['post']=array('taskid'=>$item['id']);
	exi($return);
}
if($_RQ['op']=='addlog'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$taskid=intval($post['taskid']);
		$item=pdo_get('jy_workliu_task',array('id'=>$taskid),array('id','title','tcpid'));
		$tcplog = array(
			'tcpid'=>$item['tcpid'],
			'uniacid'=>$_SESSION['uniacid'],
			'setmid'=>$_SESSION['uid'],
			'msg'=>trim($post['msg']),
			'image'=>ijson_encode($post['image']),
			'createtime'=>TIMESTAMP
		);
		pdo_insert('jy_workliu_tcplog',$tcplog);
		exi('提交成功！','','list');
	}
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_workliu_task',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	}
	exi('删除成功！');
}